package com.test.springboot.springBootUtils.service.shell;

import java.util.Collection;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicLong;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Service;

import com.test.springboot.springBootUtils.bean.Person;

@Service
public class CrmService implements InitializingBean {
	private final Map<Long, Person> people = new ConcurrentHashMap<>();
	private final AtomicBoolean connected = new AtomicBoolean();

	boolean isConnected() {
		return this.connected.get();
	}

	void Connection(String usr, String pwd) {
		this.connected.set(true);
	}

	void disconnect() {
	}

	Person findById(Long id) {
		return this.people.get(id);
	}

	Collection<Person> findByName(String name) {
		return this.people.values().stream().filter(p -> p.getName().toLowerCase().contains(name.toLowerCase())).collect(Collectors.toList());
	}

	/**
	 * 实现 InitializingBean 接口 在初始化Bean 之前 必先执行该方法
	 */
	@Override
	public void afterPropertiesSet() throws Exception {
		AtomicLong ids = new AtomicLong();
		Map<Long, Person> map = Stream.of("huyong", "lisi", "zhangsan", "wangwu").map(name -> new Person(ids.incrementAndGet(), name)).collect(Collectors.toMap(Person::getId, p -> p));
		this.people.putAll(map);

	}

}
